package com.wuzhishan.news.service.Impl;

import com.wuzhishan.news.mapper.articleMapper;
import com.wuzhishan.news.mapper.groupMapper;
import com.wuzhishan.news.mapper.userMapper;
import com.wuzhishan.news.pojo.BlogArticle;
import com.wuzhishan.news.pojo.BlogGroup;
import com.wuzhishan.news.pojo.BlogUser;
import com.wuzhishan.news.service.apiService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Service
public class apiServiceImpl implements apiService {

    @Autowired
    private articleMapper articleMapper;
    @Autowired
    private groupMapper groupMapper;
    @Autowired
    private userMapper userMapper;

    /**
     * 获取最新文章
     * @param maxResults
     * @return
     */
    @Override
    public List<BlogArticle> latests(String maxResults) {
        return articleMapper.latests(Long.parseLong(maxResults));
    }

    /**
     * 获取热门文章
     * @param maxResults
     * @return
     */
    @Override
    public List<BlogArticle> hots(String maxResults) {
        return articleMapper.hots(Long.parseLong(maxResults));
    }

    /**
     * 获取最新用户
     * @param maxResults
     * @return
     */
    @Override
    public List<BlogUser> hotusers(String maxResults) {
        return userMapper.findHotUsers(Long.parseLong(maxResults));
    }

    /**
     * 爬取文章数据接口
     * @param session
     */
    @Override
    public void getArticle(HttpSession session) throws IOException {
        //判断用户是否登录
        BlogUser loginUser = (BlogUser) session.getAttribute("loginInfo");
        if(loginUser == null){
            //用户未登录，不进行文章的插入操作
            return;
        }
        BlogArticle article = null;
        String url = "https://bbs.csdn.net";
        for (int i = 50; i < 80; i++) {
            Document document = Jsoup.connect(url + "/forums/java?page=" + i).get();
            Elements elements = document.getElementsByClass("forums_title");
            for (Element element : elements) {
                article = new BlogArticle();
                //文章标题
                article.setTitle(element.text());
                //定位到对应的文章详情页进行数据的爬取
                Document doc2 = Jsoup.connect(url+element.attr("href")).get();
                //文章内容
                Element content = doc2.getElementsByClass("post_body").first();
                content.getElementsByTag("script").remove();
                content.getElementsByTag("img").remove();
                article.setContent(content.html());
                //文章创建时间
                Element time = doc2.getElementsByClass("date_time").first();
                article.setCreateTime(time.text());
                Element reply_num = doc2.getElementsByClass("reply_num").first();
                //文章点击量
                article.setHits(Integer.parseInt(reply_num.text()) * 100);
                article.setUser(loginUser);
                //文章所属分组
                BlogGroup group = groupMapper.getOneById("4");
                article.setGroup(group);
                System.out.println(article);
                System.out.println("----------------------------------------------");
                //开始插入准备好的文章
                articleMapper.add(article);
                System.out.println("第"+i+"篇文章插入完成");
            }
        }
    }
}
